package de.tum.in.eist.im.client.GUIwidgets;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;

import de.tum.in.eist.im.client.GUI;
import de.tum.in.eist.im.shared.Group;
import de.tum.in.eist.im.shared.User;

public class Groupsettings extends GUIwidget {

	ListBox groupBox;
	ListBox memberBox;
	Button addPersonToGroup;
	Button addNewGroup;

	public Groupsettings(GUI parent) {
		setGUIandRootPanel(parent);
		groupBox = new ListBox();
		groupBox.setWidth("181px");
		groupBox.setVisibleItemCount(5);

		memberBox = new ListBox();
		memberBox.setWidth("181px");
		memberBox.setVisibleItemCount(5);

		addPersonToGroup = new Button("+ person");
		addPersonToGroup.setSize("83px", "30px");

		addNewGroup = new Button("+ group");
		addNewGroup.setSize("181px", "30px");

		addHandlersToWidgets();
	}

	private void addHandlersToWidgets() {
		addNewGroup.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String groupName = Window.prompt("Please enter a group name", "groupname");
				parentGUI.getParentIM().addGroup(groupName);
				//UserPrompt up = new UserPrompt("Please enter a group name: ", parentGUI.getParentIM());
			}
		});
		// display groupmembers on selcetion
		groupBox.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				try {
					String selectedName = groupBox.getItemText(groupBox
							.getSelectedIndex());
					parentGUI.getParentIM().updateMembers(selectedName);
				} catch (Exception e) {
				}
			}
		});
		addPersonToGroup.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				parentGUI.getParentIM().addPersonToGroup(selectionInGroupBox());
			}
		});
	}

	public void show() {
		rootPanel.add(groupBox, 10, 44);
		rootPanel.add(memberBox, 197, 44);
		rootPanel.add(addNewGroup, 10, 136);
		rootPanel.add(addPersonToGroup, 720, 44);
	}

	public void hide() {
		rootPanel.remove(groupBox);
		rootPanel.remove(memberBox);
		rootPanel.remove(addNewGroup);
		rootPanel.remove(addPersonToGroup);
	}

	public void initGroups(List<String> groupList) {
		groupList = new ArrayList<String>();
		for (String groupName : groupList) {
			addGroup(groupName);
		}
	}

	public void addGroup(String groupName) {
		groupBox.addItem(groupName);
	}

	public void removeGroup(Group g) {
		for (int i = 0; i < groupBox.getItemCount(); i++) {
			if (groupBox.getItemText(i).equals(g.getName())) {
				groupBox.removeItem(i);
				break;
			}
		}
	}

	public void updateMembers(List<User> userList) {
		memberBox.clear();
		for (User u : userList) {
			memberBox.addItem(u.getUsername());
		}
	}
	
	public String selectionInGroupBox() {
		return groupBox.getItemText(groupBox.getSelectedIndex());
	}
}
